{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Consecutive Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxPower"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #连续字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串&nbsp;<code>s</code>&nbsp;，字符串的<strong>「能量」</strong>定义为：只包含一种字符的最长非空子字符串的长度。</p>\n",
    "\n",
    "<p>请你返回字符串 <code>s</code> 的 <strong>能量</strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"leetcode\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>子字符串 \"ee\" 长度为 2 ，只包含字符 'e' 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"abbcccddddeeeeedcba\"\n",
    "<strong>输出：</strong>5\n",
    "<strong>解释：</strong>子字符串 \"eeeee\" 长度为 5 ，只包含字符 'e' 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 500</code></li>\n",
    "\t<li><code>s</code>&nbsp;只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [consecutive-characters](https://leetcode.cn/problems/consecutive-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [consecutive-characters](https://leetcode.cn/problems/consecutive-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"leetcode\"', '\"abbcccddddeeeeedcba\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "\n",
    "        n = len(s)\n",
    "        res = i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i+= 1\n",
    "            res = max(res,i-start)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        maxp = 0\n",
    "        ans = 0\n",
    "        left = 0\n",
    "        char = ''\n",
    "        while left < len(s):\n",
    "            char = s[left]\n",
    "            ans = 0\n",
    "            while s[left] == char:\n",
    "                ans+=1\n",
    "                left+=1\n",
    "                if left >= len(s):\n",
    "                    break\n",
    "            if maxp < ans:\n",
    "                maxp = ans\n",
    "        return maxp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        nu=0\n",
    "        u=0\n",
    "        for i in range(n):\n",
    "            if i==0 or s[i-1] != s[i]:\n",
    "                nu=1\n",
    "            else:\n",
    "                nu+=1  \n",
    "            u=max(nu,u)\n",
    "        return u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        l=r=ans=0\n",
    "        while l<len(s):\n",
    "            while r<len(s) and s[l]==s[r]:\n",
    "                r+=1\n",
    "            ans=max(ans,r-l)\n",
    "            l=r\n",
    "        return ans        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=1\n",
    "        count = 1\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] == s[i+1]:\n",
    "                 count+=1\n",
    "                 ans= max(ans,count)\n",
    "            else:\n",
    "                ans= max(ans,count)\n",
    "                count = 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        l, r = 0, 1\n",
    "        x = 0\n",
    "        while l < len(s):\n",
    "            t = 1 \n",
    "            while r < len(s) and s[r] == s[l]:\n",
    "                r += 1\n",
    "                t += 1\n",
    "            l = r \n",
    "            r += 1\n",
    "            x = max(x, t)\n",
    "        return x \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=1\n",
    "        count = 1\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] == s[i+1]:\n",
    "                 count+=1\n",
    "                 ans= max(ans,count)\n",
    "            else:\n",
    "                #ans= max(ans,count)\n",
    "                count = 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        left, right = 0, 0\n",
    "        current_char = s[0]\n",
    "        # 以窗口 [left, right]的形式进行遍历，保证窗口内始终是同一个字符\n",
    "        while right < len(s):\n",
    "            if s[right] == current_char:\n",
    "                right += 1\n",
    "                continue\n",
    "            # 当出现不同值的时候计算当前窗口大小,并更新窗口范围和当前字符\n",
    "            ans = max(ans, right - left)\n",
    "            left = right\n",
    "            current_char = s[right]\n",
    "        ans = max(ans, right - left)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            j = i + 1\n",
    "            while j < n and s[j] == s[i]:\n",
    "                j += 1\n",
    "            ans = max(ans,j - i)\n",
    "            i = j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # 单循环\n",
    "        res, tmp = 1, 1\n",
    "        for i in range(1, len(s)):\n",
    "            if s[i] == s[i-1]:\n",
    "                tmp += 1\n",
    "                res = max(res, tmp)\n",
    "            else:\n",
    "                tmp = 1\n",
    "        return res\n",
    "\n",
    "        # 双循环 外循环分段+计算结果 内循环判定段尾\n",
    "        res, i = 1, 1\n",
    "        while i < len(s):\n",
    "            if s[i] != s[i-1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start, i = i - 1, i + 1\n",
    "            while i < len(s) and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            res = max(res, i-start)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=cur=1\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                cur+=1\n",
    "                ans=max(ans,cur)\n",
    "            else:\n",
    "                cur=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "\"leetcode\"\n",
    "\"abbcccddddeeeeedcba\"\n",
    "\n",
    "1 <= s.length <= 500\n",
    "s consists of only lowercase English letters.\n",
    "\n",
    "String\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        pre = 0\n",
    "        res = 0\n",
    "        n = len(s)\n",
    "        for i in range(1, n+1):\n",
    "            if i == n or s[i] != s[pre]:\n",
    "                res = max(res, i-pre)\n",
    "                pre = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # 本质就是考连续字符\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        nu=1\n",
    "        u=1\n",
    "        for i in range(n):\n",
    "            if i==0 or s[i-1] != s[i]:\n",
    "                nu=1\n",
    "            else:\n",
    "                nu+=1\n",
    "                if nu > u:\n",
    "                    u=nu\n",
    "        return u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        maxp = 0\n",
    "        ans = 0\n",
    "        left = 0\n",
    "        char = ''\n",
    "        while left < len(s):\n",
    "            char = s[left]\n",
    "            ans = 0\n",
    "            while s[left] == char:\n",
    "                ans+=1\n",
    "                left+=1\n",
    "                if left >= len(s):\n",
    "                    break\n",
    "            maxp = max(maxp,ans)\n",
    "        return maxp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=1\n",
    "        i,n=0,len(s)\n",
    "        while i<n:\n",
    "            start=i\n",
    "            while i<n-1 and s[i]==s[i+1]:\n",
    "                i+=1\n",
    "                ans=max(ans,i-start+1)\n",
    "            i+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        i,n,ret=0,len(s),0\n",
    "        while i<n:\n",
    "            left=i\n",
    "            i+=1\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            ret=max(ret,i-left)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans,cnt = 1,1\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i] == s[i-1]:\n",
    "                cnt+=1\n",
    "                ans = max(ans,cnt)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        if n<2:\n",
    "            return n\n",
    "        l,r=0,1\n",
    "        res=1\n",
    "        while l<n and r<n:\n",
    "            while r<n and s[r]==s[l]:\n",
    "                r+=1\n",
    "            res=max(res,r-l)\n",
    "            l+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=i=0\n",
    "        n=len(s)\n",
    "        while i<n:\n",
    "            start=i\n",
    "            i+=1\n",
    "            while i<n and s[i]!=s[i-1]:\n",
    "                start=i\n",
    "                i+=1\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            ans=max(ans,i-start)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        res = 1\n",
    "        left = right = 0\n",
    "        while left < len(s):\n",
    "            while right < len(s) and s[right] == s[left]:\n",
    "                right += 1\n",
    "            res = max(res,right - left)\n",
    "            left = right\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        res = 1\n",
    "        left = right = 0\n",
    "        while left < len(s):\n",
    "            while right < len(s) and s[right] == s[left]:\n",
    "                right += 1\n",
    "            res = max(res,right - left)\n",
    "            left = right\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        res = 1\n",
    "        i=1\n",
    "        start = 0\n",
    "        while i<n:\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            res = max(i-start,res)\n",
    "            start = i\n",
    "            i+=1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        res = 0\n",
    "        while i<n:\n",
    "            start = i \n",
    "            i += 1\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            res = max(res, i-start)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = res = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i = i + 1\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            res = max(res, i - start)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        cur=1\n",
    "        ans=1\n",
    "\n",
    "        for i in range(1,n):\n",
    "            if s[i]==s[i-1]:\n",
    "                cur+=1\n",
    "                ans=max(ans,cur)\n",
    "            else:\n",
    "                cur=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            ans.append(i)\n",
    "        sums = 1\n",
    "        i = 1\n",
    "        while i < len(s):\n",
    "            if ans[i] != ans[i-1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i \n",
    "            i += 1\n",
    "            while i < len(s) and ans[i] == ans[i-1]:\n",
    "                i += 1\n",
    "            sums = max(sums,i-start+1)\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        pre = s[0]\n",
    "        res=1\n",
    "        tmp=1\n",
    "        for i in s[1:]:\n",
    "            if pre==i:\n",
    "                tmp+=1\n",
    "            else:\n",
    "                res=max(res,tmp)\n",
    "                tmp=1\n",
    "            pre=i\n",
    "        return max(res,tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        ans=1\n",
    "        i,j=0,0\n",
    "        while j<n:\n",
    "            if s[j]!=s[i]:\n",
    "                i=j\n",
    "                continue\n",
    "            else:\n",
    "                j+=1\n",
    "                diff=j-i\n",
    "                ans=max(ans,diff)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        dp=[1]*len(s)\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                dp[i]=dp[i-1]+1\n",
    "        return max(dp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        # cur=1\n",
    "        # ans=1\n",
    "\n",
    "        # for i in range(1,n):\n",
    "        #     if s[i]==s[i-1]:\n",
    "        #         cur+=1\n",
    "        #         ans=max(ans,cur)\n",
    "        #     else:\n",
    "        #         cur=1\n",
    "        # return ans\n",
    "        left,right=0,0\n",
    "        ans=0\n",
    "        while right<n:\n",
    "            left=right\n",
    "            right+=1\n",
    "            while right<n and s[right]==s[right-1]:\n",
    "                right+=1\n",
    "            \n",
    "            ans=max(ans,right-left)\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        res = 1\n",
    "        count = 1\n",
    "        s = list(s)\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i] == s[i-1]:\n",
    "                count += 1\n",
    "            else:\n",
    "                res = max(res,count)\n",
    "                count = 1\n",
    "        res = max(res,count)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # ret = 0\n",
    "        # n = len(s)\n",
    "        # l = 0\n",
    "        # r = 1\n",
    "        # if n == 1:\n",
    "        #     return 1\n",
    "        # while r < n:\n",
    "        #     if s[r] != s[r - 1]:\n",
    "                \n",
    "        #         l = r\n",
    "        #         r += 1\n",
    "        #     else:\n",
    "        #         r += 1\n",
    "        #     ret = max(ret, r - l)\n",
    "        # return ret\n",
    "        left = 0\n",
    "        n = len(s)\n",
    "        ret = 0\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        while left < n - 1:\n",
    "            i = left + 1\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "                \n",
    "            ret = max(ret, i - left)\n",
    "            left = i\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        maxp = 0\n",
    "        ans = 0\n",
    "        left = 0\n",
    "        char = ''\n",
    "        while left < len(s):\n",
    "            char = s[left]\n",
    "            ans = 0\n",
    "            while s[left] == char:\n",
    "                ans+=1\n",
    "                left+=1\n",
    "                if left >= len(s):\n",
    "                    break\n",
    "            maxp = max(maxp,ans)\n",
    "        return maxp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        n = len(s)\n",
    "        \n",
    "        while i<n:\n",
    "            \n",
    "            start = i\n",
    "            i+=1\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            ans =max(ans,i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        res = 0\n",
    "        left = right = 0\n",
    "        while left < len(s):\n",
    "            while right < len(s) and s[right] == s[left]:\n",
    "                right += 1\n",
    "            res = max(res,right - left)\n",
    "            left = right\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # n=len(s)\n",
    "        # i=0\n",
    "        # ans=0\n",
    "        # while i<n:\n",
    "        #     start=i \n",
    "        #     i+=1\n",
    "        #     while i<n and s[i]==s[i-1]:\n",
    "        #         i+=1\n",
    "        #     ans=max(ans,i-start)\n",
    "        # return ans\n",
    "        left,right =0,0\n",
    "        n=len(s)\n",
    "        ans=0\n",
    "        while left<n:\n",
    "            while right<n and s[left]==s[right]:\n",
    "                right+=1\n",
    "            ans=max(ans,right-left)\n",
    "            left = right\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        nu=1\n",
    "        u=1\n",
    "        for i in range(n):\n",
    "            if i==0 or s[i-1] != s[i]:\n",
    "                nu=1\n",
    "            else:\n",
    "                nu+=1\n",
    "                if nu > u:\n",
    "                    u=nu\n",
    "        return u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = 1\n",
    "        i = start = 0\n",
    "        # s = list(s)\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            if i + 1 == n:\n",
    "                if n==1 or s[i - 1] == s[i]:\n",
    "                    return max(ans, i - start + 1)\n",
    "                else: return ans\n",
    "            elif s[i] != s[i + 1]:\n",
    "                i += 1                \n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i + 1 < n and s[i] == s[i + 1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start + 1)\n",
    "        return ans\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        l=0\n",
    "        n=len(s)\n",
    "\n",
    "        ans=0\n",
    "\n",
    "        while l<n:\n",
    "            r=l+1\n",
    "            while r<=n-1 and s[r]==s[l]:\n",
    "                r+=1\n",
    "            \n",
    "            ans=max(ans,(r-1)-l+1)\n",
    "            \n",
    "            l=r\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        \n",
    "        res= left = right = 0\n",
    "        while left < len(s):\n",
    "            while right < len(s) and s[right] == s[left]:\n",
    "                right += 1\n",
    "            res = max(res,right - left)\n",
    "            left = right\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        if len(s)==1:\n",
    "            return 1\n",
    "        current_power=1\n",
    "        max_power=1\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                current_power+=1\n",
    "            else:\n",
    "                max_power=max(current_power,max_power)\n",
    "                current_power=1\n",
    "        return max(current_power,max_power)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans=1\n",
    "        n=len(s)\n",
    "        cur=1\n",
    "\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                cur+=1\n",
    "                ans=max(ans,cur)\n",
    "            else:\n",
    "                cur=1\n",
    "        \n",
    "        return ans\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i + 1 < n and s[i + 1] == s[i]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start + 1)\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "         # 初始化最大连续字符数和当前连续字符数\n",
    "        max_count = 1\n",
    "        count = 1\n",
    "        # 遍历字符串，从第二个字符开始比较\n",
    "        for i in range(1, len(s)):\n",
    "            # 如果当前字符和前一个字符相同，则当前连续字符数加1\n",
    "            if s[i] == s[i-1]:\n",
    "                count += 1\n",
    "            # 如果当前字符和前一个字符不同，则更新最大连续字符数和当前连续字符数\n",
    "            else:\n",
    "                max_count = max(max_count, count)\n",
    "                count = 1\n",
    "        # 返回最大连续字符数和当前连续字符数中的较大值\n",
    "        return max(max_count, count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            ans.append(i)\n",
    "        sums = 1 # 至少有一个字符算为相应长度\n",
    "        i = 1\n",
    "        while i < len(s):\n",
    "            if ans[i] != ans[i-1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i \n",
    "            i += 1\n",
    "            while i < len(s) and ans[i] == ans[i-1]:\n",
    "                i += 1\n",
    "            sums = max(sums,i-start+1)\n",
    "        return sums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i-1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s) :\n",
    "        count=1\n",
    "        countlist=[]\n",
    "        if len(s)<2:\n",
    "            maxpower=len(s)\n",
    "            return maxpower\n",
    "        if len(s)==2:\n",
    "            if s[0]==s[1]:\n",
    "                return 2\n",
    "        for i in range(0,len(s)-1):\n",
    "            if s[i]==s[i+1]:\n",
    "                count+=1\n",
    "                continue\n",
    "            else:\n",
    "                countlist.append(count)\n",
    "                count=1\n",
    "        countlist.append(count)\n",
    "        maxpower=countlist[0]\n",
    "        for i in countlist:\n",
    "            if i>maxpower:\n",
    "                maxpower=i\n",
    "        return maxpower\n",
    "       \n",
    "   \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        if len(s) == 1:\n",
    "            return 1\n",
    "\n",
    "        left = 0\n",
    "        right = 1\n",
    "        ans = 0\n",
    "\n",
    "        while right != len(s):\n",
    "            if s[left] != s[right]:\n",
    "                ans = max(ans, right - left)\n",
    "                left = right\n",
    "                right = right + 1\n",
    "            else:\n",
    "                right += 1\n",
    "        \n",
    "        ans = max(ans, right - left)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans, cnt = 1, 1\n",
    "        for i in range(1, len(s)):\n",
    "            if s[i] == s[i-1]:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        return ans\n",
    "            \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        fast = slow = ans = 0\n",
    "         \n",
    "        n=len(s)\n",
    "        while slow <n:\n",
    "            while fast < n and s[fast] == s[slow]:\n",
    "                fast += 1\n",
    "            ans = max(ans, fast - slow)\n",
    "            slow = fast\n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # 分组循环\n",
    "        # 好处：每个逻辑块只负责各自对应的工作，方便管理\n",
    "\n",
    "        n = len(s)\n",
    "        ans, i = 1, 1\n",
    "\n",
    "        while i < n:\n",
    "            # 分组前的准备工作，起始位置的指针，以及统计答案的工作\n",
    "            if s[i] != s[i - 1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i - 1\n",
    "            i += 1\n",
    "\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            \n",
    "            ans = max(ans, i - start)\n",
    "        \n",
    "        return ans\n",
    "\n",
    "    def maxPower2(self, s):\n",
    "        # 直接遍历也可以\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = 1\n",
    "        i = start = 0\n",
    "        # s = list(s)\n",
    "        n = len(s)\n",
    "        while i < n - 1:            \n",
    "            if s[i] != s[i + 1]:\n",
    "                i += 1                \n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i + 1 < n and s[i] == s[i + 1]:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start + 1)\n",
    "        return ans\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower1(self, s: str) -> int:\n",
    "        # 分组循环\n",
    "        # 好处：每个逻辑块只负责各自对应的工作，方便管理\n",
    "\n",
    "        n = len(s)\n",
    "        ans, i = 1, 1\n",
    "\n",
    "        while i < n:\n",
    "            # 分组前的准备工作，起始位置的指针\n",
    "            if s[i] != s[i - 1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i - 1\n",
    "            i += 1\n",
    "\n",
    "            # 组内循环\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            \n",
    "            # 负责统计结果\n",
    "            ans = max(ans, i - start)\n",
    "        \n",
    "        return ans\n",
    "\n",
    "    def maxPower(self, s):\n",
    "        # 直接遍历也可以\n",
    "        # 这个想法确实不太容易想到，所以分组循环思路特别重要\n",
    "        ans, cnt, n = 1, 1, len(s)\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if s[i] == s[i - 1]:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        fast = slow = ans = 0\n",
    "         \n",
    "        n=len(s)\n",
    "        while slow <n:\n",
    "            while fast < n and s[fast] == s[slow]:\n",
    "                fast += 1\n",
    "            ans = max(ans, fast - slow)\n",
    "            slow = fast\n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            temp=1\n",
    "            st=i\n",
    "            i+=1\n",
    "            while i<n and s[i]==s[i-1]:\n",
    "                i+=1\n",
    "            ans=max(ans,i-st)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        # n = len(s)\n",
    "        # # if n == 1:\n",
    "        # #     return 1\n",
    "        # result = 0\n",
    "        # for i in range(n):\n",
    "        #     temp = 0\n",
    "        #     for j in range(i, n):\n",
    "        #         if s[j] != s[i]:\n",
    "        #             result = max(result, temp)\n",
    "        #             break\n",
    "        #         else:\n",
    "        #             temp += 1\n",
    "        # return max(result, temp)\n",
    "        ans, cnt = 1, 1\n",
    "        for i in range(1, len(s)):\n",
    "            if s[i] == s[i - 1]:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        maxp,i,j = 1,0,1\n",
    "        while i<len(s)-1:\n",
    "            while j<len(s) and s[i] == s[j]:\n",
    "                j += 1                \n",
    "            if maxp<j-i: maxp = j-i\n",
    "            i = j\n",
    "            j += 1\n",
    "        return maxp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        len_,a,b=len(s),1,1\n",
    "        for i in range(1,len_):\n",
    "            if s[i]==s[i-1]:\n",
    "                a+=1\n",
    "            else:\n",
    "                if a>b:\n",
    "                    b=a\n",
    "                a=1\n",
    "        return max(b,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        fast = slow =  0\n",
    "        ans = 1\n",
    "        n=len(s)\n",
    "        while slow <n:\n",
    "            while fast < n and s[fast] == s[slow]:\n",
    "                fast += 1\n",
    "            ans = max(ans, fast - slow)\n",
    "            slow = fast\n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = count = 1\n",
    "        for i in range(1, len(s)):\n",
    "            if s[i] == s[i - 1]:\n",
    "                count += 1\n",
    "            else:\n",
    "                count = 1\n",
    "            ans = max(ans, count)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxPower(self, s: str) -> int:\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            ans.append(i)\n",
    "        sums = 1 # 至少有一个字符算为相应长度\n",
    "        i = 1\n",
    "        while i < len(s):\n",
    "            if ans[i] != ans[i-1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i \n",
    "            i += 1\n",
    "            while i < len(s) and ans[i] == ans[i-1]:\n",
    "                i += 1\n",
    "            sums = max(sums,i-start+1)\n",
    "        return sums\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
